package com.example.musicplayer.controller;

import com.example.musicplayer.mapper.LoveMusicMapper;
import com.example.musicplayer.model.Music;
import com.example.musicplayer.model.User;
import com.example.musicplayer.tools.Constant;
import com.example.musicplayer.tools.ResponseBodyMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/lovemusic")

public class LoveMusicController {
    @Resource
    private LoveMusicMapper loveMusicMapper;

    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id,
                                                  HttpServletRequest req) {
        int musicId = Integer.parseInt(id);
        System.out.println("likeMusic->musicId: "+musicId);
        //没有session不创建
        HttpSession httpSession = req.getSession(false);
        if(httpSession == null ||
                httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }
        User user =
                (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("likeMusic->userID: "+userId);
        //查询当前⽤⼾是否点赞过该⾳乐
        Music music =
                loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId,musicId);
        if(music!=null) {
            return new ResponseBodyMessage<>(-1,"该⽤⼾点赞过该⾳乐",false);
        }else {
            boolean effect = loveMusicMapper.insertLoveMusic(userId,musicId);
            if(effect) {
                return new ResponseBodyMessage<>(0,"点赞⾳乐成功",true);
            }else {
                return new ResponseBodyMessage<>(-1,"点赞⾳乐失败",false);
            }
        }
    }

    @RequestMapping("/findlovemusic")

    public ResponseBodyMessage<List<Music>>

    findLoveMusic(@RequestParam(required=false)String musicName,
                  HttpServletRequest req) {
        //没有session不创建
        HttpSession httpSession = req.getSession(false);
        if(httpSession == null ||
                httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1,"没有登录",null);
        }
        User user = (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userid = user.getId();
        List<Music> musics = new ArrayList<>();
        if(musicName != null) {
            musics = loveMusicMapper.findLoveMusicBykeyAndUID(musicName,userid);
        }else {
            musics = loveMusicMapper.findLoveMusicByUserId(userid);
        }
        return new ResponseBodyMessage<>(0,"查询到了所有的收藏的⾳乐",musics);
    }

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id, HttpServletRequest req) {
        int musicId = Integer.parseInt(id);
        //没有session不创建
        HttpSession httpSession = req.getSession(false);
        if(httpSession == null ||
                httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }
        User user = (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userid = user.getId();
        int ret = loveMusicMapper.deleteLoveMusic(userid,musicId);
        if(ret == 1) {
            return new ResponseBodyMessage<>(0,"取消收藏成功！",true);
        }else {
            return new ResponseBodyMessage<>(0,"取消收藏失败！",false);
        }
    }



}

